#include "player.h"


player::player() : keyboard(nullptr), positionQueue(nullptr)
{
	x = -1;
	y = -1;
	id = -1;
}

player::player(int x, int y, int id, deque<char>* k, deque<pos>* p) : keyboard(k), positionQueue(p)
{
	this->x = x;
	this->y = y;
	this->id = id;	
}

player::~player()
{
}

void player::operator()()
{
	while (true)
	{
		mutex_keyboard[id].lock();
		while (!keyboard->empty())
		{
			char input = keyboard->front();
			keyboard->pop_front();
			switch (input)
			{
			case 'a':
				if (x > 0)
					x--;
				break;
			case 'd':
				if (x < 7)
					x++;
				break;
			case 'w':
				if (y > 0)
					y--;
				break;
			case 's':
				if (y < 7)
					y++;
				break;
			}
			mutex_pos.lock();
			positionQueue->push_back(pos(x, y, id));
			mutex_pos.unlock();
		}
		mutex_keyboard[id].unlock();
	}	
}
